{include file="common/header"/}
<style>
    .layui-input, .layui-textarea {
        width: 300px
    }

    .layui-input, .layui-textarea {
        display: inline-block;
    }
</style>
<div class="lemo-container">
  <div class="lemo-main">
      <div class="admin-main layui-anim layui-anim-upbit">
          <fieldset class="layui-elem-field layui-field-title">
              <legend>添加 sku</legend>
          </fieldset>
      <form class="layui-form layui-form-pane" action="" lay-filter="formFilter">
        <input type="hidden" id="customer_id" value="{$id}">
          <div class="layui-form-item">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-block">
              <input type="number" id="num" name="num" max="9999" min="1" step="1"   placeholder="请输入金额" autocomplete="off" class="layui-input">
            </div>
          </div>


          <div class="layui-form-item">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-block">
              <input type="number" id="price" name="price" placeholder="请输入材质" autocomplete="off" class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">运费</label>
            <div class="layui-input-block">
              <input type="number" id="shipping_fee" name="shipping_fee"  lay-verify="required" placeholder="请输入运费" autocomplete="off" class="layui-input">
              <!-- <input type="checkbox" id="sf_lock" name="sf_lock" title="锁定" lay-skin="tag"> -->
            </div>
          </div>



        <div class="layui-form-item">
          <div class="layui-input-inline">
              <input type="hidden" name="id"  >
              <button type="button" class="layui-btn layui-bg-orange" id="addsku">添加</button>

              <button type="button" class="layui-btn" lay-filter="submitsku" id="submitsku">{:lang('submit')}</button>
          </div>
      </div>
        </form>

        <table class="layui-hide" id="sku_table"></table>
          
  </div>
  </div>
</div>
  {include file="common/footer"/}

<script>
  layui.config({
      base: '/static/admin/js/',
      version: true
  }).extend({
      Admin: 'Admin',
  }).use(['form','Admin', 'laydate'], function () {

      var form = layui.form, layer = layui.layer,$= layui.jquery, table = layui.table, util = layui.util, laydate = layui.laydate;
      var info = '';
      var table_data = [];

      function success (title) {
            layer.msg('操作成功', {icon: 1, time: 1500}, function() {
                  parent.location.href = 'productList'; // 父页面跳转
              });
           
      };
      
      $('#submitsku').click(() => {
        
        var cache = table.cache['sku_table_cache'];

        $.post('addSku',{data: cache},function(res){
                        console.log(res);
                        if(res.code === 1){
                            success(res.msg);
                            window.parent.location.href = 'customer?id=' + res.id;            
                        }else{
                            error(res.msg);
                        }
                    });
      });

      $('#addsku').click(() => {
        const cache = table.cache['sku_table_cache'];
        var num = $('#num').val();
        if (!Number.isInteger(parseInt(num))) {
            console.log(typeof num);
            layer.tips('数量必须是整数', '#num', {
                tips: 1
            })
            return false;
        }

        var price = $('#price').val();
        if (!Number(price)) {
            layer.tips('价格参数不合法', '#num', {
                tips: 1
            });
            return false;
        }
        var customer_id = $('#customer_id').val();
        var shipping_fee = $('#shipping_fee').val();
        cache.push({
            num,
            price,
            shipping_fee,
            customer_id,
        });
        table.reload('sku_table_cache', { 
            data: cache 
        });
        var filter = {
            num: '',
            price: '',
        }
        // var is_unit_lock = $('#unit_lock');
        // var is_sf_lock = $('#sf_lock');
        // var is_price_adjustment_date_lock = $('#price_adjustment_date');
        // console.log(is_sf_lock.prop('checked'), is_unit_lock.prop('checked'));
        // if (!is_sf_lock.prop('checked')) {
        //     filter.shipping_fee = '';
        // }
        // if (!is_unit_lock.prop('checked')) {
        //     filter.unit = '';
        // }
        // if (!is_price_adjustment_date_lock.prop('checked')) {
        //   filter.price_adjustment_date = '';
        // }
        form.val('formFilter', filter);
        form.render();
    });

    table.render({
        elem: '#sku_table',
        id: 'sku_table_cache',
        cols: [[ //标题栏
            {field: 'num', title: '数量', width: '33.33%', sort: true},
            {field: 'price', title: '价格', width: '33.33%'},
            {field: 'shipping_fee', title: '运费', width: '33.33%'},
        ]],
        data: table_data,
    });

      form.render();
      laydate.render({
        elem: '#ID-laydate-demo'
      });
  });

  
</script>